package com.bonc.modules.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bonc.common.utils.*;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.business.entity.TbCommonParamInfoEntity;
import com.bonc.modules.business.entity.TbWorkOrderUrgeEntity;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bonc.modules.business.dao.TbWorkOrderInfoDao;
import com.bonc.modules.business.entity.TbWorkOrderInfoEntity;
import com.bonc.modules.business.service.TbWorkOrderInfoService;

import javax.xml.ws.Action;

@DataSource("data")
@Service("tbWorkOrderInfoService")
public class TbWorkOrderInfoServiceImpl extends ServiceImpl<TbWorkOrderInfoDao, TbWorkOrderInfoEntity> implements TbWorkOrderInfoService {

    @Autowired
    private TbWorkOrderInfoService tbWorkOrderInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbWorkOrderInfoEntity> page = this.page(
                new Query<TbWorkOrderInfoEntity>().getPage(params),
                new QueryWrapper<TbWorkOrderInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(TbWorkOrderInfoEntity tbWorkOrderInfo) {
        return SqlHelper.retBool(this.getBaseMapper().insert(tbWorkOrderInfo));
    }

    @Override
    public boolean updateById(TbWorkOrderInfoEntity tbWorkOrderInfo) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(tbWorkOrderInfo));
    }

    @Override
    public TbWorkOrderInfoEntity getById(String id) {
        return this.getBaseMapper().selectById(id);
    }

    @Override
    public void updateStatus(Map<String, Object> map) {
        this.baseMapper.updateStatus(map);
    }

    @Override
    public void updateHandingStatus(String identifier) {
        this.baseMapper.updateHandingStatus(identifier);
    }

    @Override
    public AtomicReference<String> postOrderInfoService(String identifier) {
        /*LambdaQueryWrapper<TbWorkOrderInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<TbWorkOrderInfoEntity> list = tbWorkOrderInfoService.list(queryWrapper);*/
        AtomicReference<String> res = new AtomicReference<>("");
        AtomicReference<String> req = new AtomicReference<>("");

        /*list.forEach(orderInfo->{

        });*/

        String url = "http://10.254.30.252:3900/api/v2/sync/CSVC/QueryCSS";
        //String url = "http://localhost:9000/quality-enhance/business/tbworkorderinfo/QueryCSS";
        SupermanUtil util = new SupermanUtil();

        JSONObject content = new JSONObject();
        /*content.put("Identifier", orderInfo.getIdentifier());*/
        content.put("Identifier", identifier);

        Map<String, Object> param = ResolveParamUtils.getAllParam(content);

        req.set(JSON.toJSONString(param));
        res.set(util.sendPost(url, req.get()));

        return res;
    }

    @Override
    public ReplyCommon QueryCSS(TbCommonParamInfoEntity tbCommonParamValue) {
        String content = "";
        String rspCode = "";
        String rspDesc = "";
        JSONObject res = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMddHHmmss");
        String cutOffDay = dateFormat.format(calendar.getTime());
        String handingTime = dateFormat1.format(calendar.getTime());
        String transIDO = handingTime + System.currentTimeMillis();
        String providePartyID = "CSVB3510";
        String transIDHTime = dateFormat1.format(calendar.getTime());
        String transIDH = transIDO;

        /*String signjy = ResolveParamUtils.analysisAllParam(tbCommonParamValue);
        String sign = tbCommonParamValue.getSign();
        if(sign == signjy || sign.equals(signjy)){

        }else{
            rspCode = "20207";
            rspDesc = "非法的签名";
        }*/
        content = tbCommonParamValue.getContent();
        TbWorkOrderInfoEntity tbWorkOrderInfo = JSONObject.parseObject(content,TbWorkOrderInfoEntity.class);
        if(StringUtils.isBlank(tbWorkOrderInfo.getIdentifier())){
            rspCode = "20101";
            rspDesc = "缺少必须的参数";
        }else{
            tbWorkOrderInfo = tbWorkOrderInfoService.getById(tbWorkOrderInfo.getIdentifier());
            if(tbWorkOrderInfo != null){
                rspCode = "00000";
                rspDesc = "success";
                Map resMap = new HashMap();
                resMap.put("HandingTime", tbWorkOrderInfo.getHandingTime());
                resMap.put("Handler", tbWorkOrderInfo.getHandler());
                resMap.put("HandingDepartment", tbWorkOrderInfo.getHandingDepartment());
                resMap.put("HandlerContactInfor", tbWorkOrderInfo.getHandlerContactInfor());
                resMap.put("HandingOpinions", tbWorkOrderInfo.getHandingOpinions());
                res.put("Identifier", tbWorkOrderInfo.getIdentifier());
                res.put("State", tbWorkOrderInfo.getState());
                jsonArray = JSONArray.fromObject(resMap);
                res.put("ExtIdentylogList",jsonArray);
            }else{
                rspCode = "20000";
                rspDesc = "查无此单";
            }
        }
        return ReplyCommon.ok(transIDO,cutOffDay,transIDH,transIDHTime,providePartyID,rspCode,rspDesc).put("result", res.toString());
    }

}
